package com.zp.business.impl.infusion;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.business.entity.infusion.InfusionType;
import com.zp.business.entity.infusion.bo.InfusionTypeBo;
import com.zp.business.entity.infusion.vo.InfusionTypeVo;
import com.zp.business.mapper.infusion.InfusionTypeMapper;
import com.zp.business.service.infusion.IInfusionTypeService;
import com.zp.common.core.constant.CacheNames;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.common.redis.utils.CacheUtils;
import com.zp.system.api.RemoteOrgDictDataService;
import com.zp.system.api.entity.SysOrg;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 输液方式配置Service业务层处理
 *
 * @author zhang peng
 * @ date 2025-02-01
 */
@RequiredArgsConstructor
@Service
public class InfusionTypeServiceImpl implements IInfusionTypeService {

    static Map<String, SysOrg> sysOrgMap = CacheUtils.get(CacheNames.SYS_ORG);
    private final InfusionTypeMapper baseMapper;
    @DubboReference
    private RemoteOrgDictDataService orgDictDataService;

    /**
     * 查询输液方式配置
     */
    @Override
    public InfusionTypeVo queryById(String infusionTypeId) {
        return baseMapper.selectVoById(infusionTypeId);
    }

    /**
     * 查询输液方式配置列表(分页)
     */
    @Override
    public TableDataInfo<InfusionTypeVo> queryPageList(InfusionTypeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<InfusionType> lqw = buildQueryWrapper(bo);
        Page<InfusionTypeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询输液方式配置列表(不分页)
     */
    @Override
    public List<InfusionTypeVo> queryList(InfusionTypeBo bo) {
        LambdaQueryWrapper<InfusionType> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<InfusionType> buildQueryWrapper(InfusionTypeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<InfusionType> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getInfusionType()), InfusionType::getInfusionType, bo.getInfusionType());
        lqw.eq(StringUtils.isNotBlank(bo.getChannel()), InfusionType::getChannel, bo.getChannel());
        lqw.eq(StringUtils.isNotBlank(bo.getOrgId()), InfusionType::getOrgId, bo.getOrgId());
        lqw.eq(StringUtils.isNotBlank(bo.getRemarks()), InfusionType::getRemarks, bo.getRemarks());
        lqw.like(StringUtils.isNotBlank(bo.getInfusionTypeName()), InfusionType::getInfusionTypeName, bo.getInfusionTypeName());
        lqw.like(StringUtils.isNotBlank(bo.getChannelName()), InfusionType::getChannelName, bo.getChannelName());
        lqw.like(StringUtils.isNotBlank(bo.getOrgName()), InfusionType::getOrgName, bo.getOrgName());
        return lqw;
    }

    /**
     * 新增输液方式配置
     */
    @Override
    public Boolean insertByBo(InfusionTypeBo bo) {
        InfusionType add = BeanUtil.toBean(bo, InfusionType.class);
        if (StringUtils.isNotBlank(add.getChannel())) {
            // 途径名称
        }
        add.setOrgName(!sysOrgMap.isEmpty() && StringUtils.isNotBlank(bo.getOrgId())
                ? sysOrgMap.get(bo.getOrgId()).getOrgName() : "");
        add.initEntity();
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setInfusionTypeId(add.getInfusionTypeId());
        }
        return flag;
    }

    /**
     * 修改输液方式配置
     */
    @Override
    public Boolean updateByBo(InfusionTypeBo bo) {
        InfusionType update = BeanUtil.toBean(bo, InfusionType.class);
        if (StringUtils.isNotBlank(update.getChannel())) {
            // 途径名称
        }
        update.setOrgName(!sysOrgMap.isEmpty() && StringUtils.isNotBlank(bo.getOrgId())
                ? sysOrgMap.get(bo.getOrgId()).getOrgName() : "");
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(InfusionType entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除输液方式配置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
